package cn.echo.common.basetest;


import org.apache.log4j.Logger;
import org.apache.log4j.net.SyslogAppender;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath*:spring*.xml")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BaseTest{
    // 模拟request,response
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;
    @Autowired
    protected WebApplicationContext wac;

    protected MockMvc mockMvc;
    protected Class className = this.getClass();
    protected Logger log = Logger.getLogger(className);
    private long beginTime;
    private long endTime;
    /**
     * 测试接口必须配置WEB上下文环境这样ServletContext这样的
     * 接口才可以被注入
     */
    @Before
    public void aBeginInit() {
        initBeginProperty();
        printBeginTipLog();
    }
    private void initBeginProperty(){
        //构造一个上下文环境原本在tomcat下面才有因为写测试代码不需要运行tomcat
        //没有上下文环境就没有ServletContext这个对象
        this.mockMvc = webAppContextSetup(this.wac).build();
        beginTime = System.currentTimeMillis();
        request = new MockHttpServletRequest();
        request.setCharacterEncoding("utf-8");
        response = new MockHttpServletResponse();

    }
    private void printBeginTipLog() {
        log.debug("*************************************************************************************************************************************");
        log.debug("***************************************Welcome come to use Junit Test Environtments!**************************************");
        log.debug("******************************************************Environments is Ok!************************************************************");
        log.debug("***********************begin to test "+className+"***********************");
        log.debug("*****************************************************no bug no bug no bug**************************************************************");
        log.debug("*************************************************begin time: "+beginTime/1000+"秒*********************************************************************");
        log.debug("*************************************************************************************************************************************");

    }

    @After
    public void zEnding(){
        initEndProperty();
        printEndTipsLog();
    }

    private void initEndProperty() {
        endTime = System.currentTimeMillis();
    }

    private void printEndTipsLog() {
        log.debug("*************************************************************************************************************************************");
        log.debug("***************************************Thanks to use Junit Test Environtments!**************************************");
        log.debug("******************************************************Environments is Ok!************************************************************");
        log.debug("***********************end to test "+className+"***********************");
        log.debug("*****************************************************You can look up to find bugs**************************************************************");
        log.debug("*************************************************end time:"+(endTime/1000)+"秒******************************************************************************");
        log.debug("*************************************************************************************************************************************");

    }


}
